<?php


namespace App\Admin\Actions\Review;


use App\Admin\Actions\RowAction;
use App\Setting;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class ReviewPost extends RowAction
{
    public $name = '审核意见';

    public function handle($model, Request $request)
    {
        $status = $request->get('status');
        $remark = $request->get('remark') ?: '';
        try {
            $model->review_status = $status;
            $model->review_remark = $remark;
            $model->save();
            if (method_exists($model, 'remarks')) {
                $review_name = Setting::getReviewName(1);
                $model->remarks()->create([
                    'name' => $review_name . '-' . data_get([1 => '同意申请', 2 => '不同意申请', 3 => '退回修改'], $status),
                    'user_id' => Admin::user()->id,
                    'remark' => $remark
                ]);
            }
        } catch (\Exception $exception) {
            return $this->response()->swal()->error('操作失败！')->refresh();
        }
        return $this->response()->success('审核成功！')->location('');
    }

    public function form()
    {
        $this->radio('status', '审核')->options([1 => '同意申请', 2 => '不同意申请', 3 => '退回修改'])->required();
        $this->textarea('remark', '备注');
    }

}
